package com.deer.backup.utils;

import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.cef.CefApp;
import org.cef.CefApp.CefAppState;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import org.springframework.boot.SpringApplication;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.deer.backup.BackupApplication;

import cn.hutool.core.img.ImgUtil;
import me.friwi.jcefmaven.CefAppBuilder;
import me.friwi.jcefmaven.MavenCefAppHandlerAdapter;

public class JframeUtils {

	private static CefApp cefApp;
	private static CefClient cefClient;

	public static void initCefClient() {
		try {
			CefAppBuilder builder = new CefAppBuilder();
			// 使用华为镜像进行下载
			builder.setMirrors(Arrays.asList("http://mirrors.huaweicloud.com/repository/maven/me/friwi/jcef-natives-{platform}/{tag}/jcef-natives-{platform}-{tag}.jar"));
			builder.getCefSettings().windowless_rendering_enabled = false;
			builder.setAppHandler(new MavenCefAppHandlerAdapter() {
				@Override
				public void stateHasChanged(CefAppState state) {
					if (state == CefAppState.TERMINATED)
						System.exit(0);
				}
			});
			cefApp = builder.build();
			cefClient = cefApp.createClient();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createJframe(JframeConfig config) {
		CefBrowser browser = cefClient.createBrowser(config.getUrl(), false, false);
		Component browerUI = browser.getUIComponent();
		SwingUtilities.invokeLater(() -> {
			JFrame frame = new JFrame();
			
			frame.setTitle(config.getTitle());
			frame.setUndecorated(config.getUndecorated()); // 取消窗口边框和标题栏显示
			
			ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

			// 本地静态资源路径
		    String filePath = "/static/deer.png";
		    // 获取指定路径下的资源文件
		    try {
				InputStream file = resourcePatternResolver.getResource(filePath).getInputStream();
				BufferedImage image = ImgUtil.read(file);
		        frame.setIconImage((Image)image);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			int width = config.getWidth();
			int height = config.getHeight();
			int x = (Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2;
			int y = (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2;
			frame.setLocation(x, y);
			frame.add(browerUI);
			frame.setSize(width, height);
			frame.setVisible(true);

			frame.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					// 释放窗口资源
					frame.dispose();
					if (config.getAllClose()) {
						// 关闭内核资源
						CefApp.getInstance().dispose();
						// 关闭后台主程序
						if (BackupApplication.context != null && BackupApplication.context.isActive()) {
							SpringApplication.exit(BackupApplication.context);
						}
					}

				}
			});
		});
	}
	
}
